package com.sloth.bear.controller;

import com.sloth.bear.annotation.NoAuth;
import com.sloth.bear.dto.base.ResponsePage;
import com.sloth.bear.dto.request.FileQueryRequest;
import com.sloth.bear.dto.request.VideoProgressRequest;
import com.sloth.bear.dto.vo.FileIndexVO;
import com.sloth.bear.dto.vo.FileUploadVo;
import com.sloth.bear.service.FileIndexService;
import com.sloth.bear.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件管理
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;
    private final FileIndexService fileIndexService;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @NoAuth
    public FileUploadVo uploadFile(@RequestParam MultipartFile file, String directory,Boolean isRename) {
        return fileService.uploadFile(file, directory,isRename);
    }

    /**
     * 文件列表
     */
    @GetMapping("/list")
    public ResponsePage<FileIndexVO> listFile(FileQueryRequest request) {
        return fileIndexService.listFile(request);
    }

    /**
     * 手动扫描指定目录
     */
    @PostMapping("/scan")
    public void scanDirectory(@RequestParam String directory) {
        fileIndexService.scanDirectory(directory);
    }

    /**
     * 扫描所有配置的目录
     */
    @PostMapping("/scan-all")
    public void scanAllDirectories() {
        fileIndexService.scanAllDirectories();
    }

    /**
     * 刷新缓存
     */
    @PostMapping("/refresh-cache")
    public void refreshCache() {
        fileIndexService.refreshCache();
    }

    /**
     * 获取扫描统计信息
     */
    @GetMapping("/stats")
    public String getScanStats() {
        return fileIndexService.getScanStatistics();
    }

    /**
     * 根据ID获取文件详情
     */
    @GetMapping("/detail/{id}")
    public FileIndexVO getFileDetail(@PathVariable Long id) {
        return fileIndexService.getFileDetail(id);
    }

    /**
     * 保存视频播放进度
     */
    @PostMapping("/progress/save")
    public void saveVideoProgress(@RequestBody VideoProgressRequest request) {
        fileIndexService.saveVideoProgress(request);
    }

    /**
     * 随机获取一个视频文件
     */
    @GetMapping("/random-video")
    public FileIndexVO getRandomVideo() {
        return fileIndexService.getRandomVideo();
    }

    /**
     * 刮削所有视频文件的元数据
     */
    @PostMapping("/scrape-all")
    public void scrapeAllVideos() {
        fileIndexService.scrapeAllVideos();
    }

    /**
     * 恢复已删除的文件记录
     * 检查已删除的记录，如果文件在磁盘上存在，则恢复记录
     */
    @PostMapping("/restore-deleted")
    public void restoreDeletedFiles() {
        fileIndexService.restoreDeletedFiles();
    }

    /**
     * 清理已删除的文件记录
     * 物理删除所有标记为 deleted=1 的记录
     */
    @PostMapping("/clean-deleted")
    public void cleanDeletedFiles() {
        fileIndexService.cleanDeletedFiles();
    }

    /**
     * 强制全量刷新
     * 重新扫描所有配置的目录，按文件修改时间从新到旧扫描
     * 不删除现有记录，只添加新文件和更新已有文件
     */
    @PostMapping("/force-full-refresh")
    public void forceFullRefresh() {
        fileIndexService.forceFullRefresh();
    }
}